<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
            <el-input v-model="listQuery.qrCode" clearable class="filter-item" style="width: 200px;" placeholder="请输入标签代码"/>
        <el-input v-model.number="listQuery.wasteId" clearable class="filter-item" style="width: 200px;" placeholder="请输入危废编号"/>
        <el-input v-model.number="listQuery.caskId" clearable class="filter-item" style="width: 200px;" placeholder="请输入容器类别"/>
      <el-input v-model="listQuery.fmacno" clearable class="filter-item" style="width: 200px;" placeholder="请输入容器序列号"/>
      <el-input v-model="listQuery.dangerType" clearable class="filter-item" style="width: 200px;" placeholder="请输入危险类别"/>
      <el-input v-model="listQuery.component" clearable class="filter-item" style="width: 200px;" placeholder="请输入主要成分"/>
      <el-input v-model="listQuery.chanshengWeight" clearable class="filter-item" style="width: 200px;" placeholder="请输入入库重量"/>
      <el-input v-model="listQuery.chukuWeight" clearable class="filter-item" style="width: 200px;" placeholder="请输入出库重量"/>
        <el-input v-model.number="listQuery.isChansheng" clearable class="filter-item" style="width: 200px;" placeholder="请输入是否产生"/>
        <el-input v-model.number="listQuery.isRuku" clearable class="filter-item" style="width: 200px;" placeholder="请输入是否入库"/>
        <el-input v-model.number="listQuery.isChuku" clearable class="filter-item" style="width: 200px;" placeholder="请输入是否出库"/>
        <el-input v-model.number="listQuery.isBaofei" clearable class="filter-item" style="width: 200px;" placeholder="请输入是否报废"/>
      <el-input v-model="listQuery.rukuPerson" clearable class="filter-item" style="width: 200px;" placeholder="请输入入库人"/>
      <el-date-picker v-model="listQuery.rukuDate" type="datetime" placeholder="请输入入库时间"></el-date-picker>
        <el-input v-model.number="listQuery.rukuId" clearable class="filter-item" style="width: 200px;" placeholder="请输入对应入库台账id"/>
      <el-input v-model="listQuery.chukuPerson" clearable class="filter-item" style="width: 200px;" placeholder="请输入出库人"/>
      <el-date-picker v-model="listQuery.chukuDate" type="datetime" placeholder="请输入出库日期"></el-date-picker>
        <el-input v-model.number="listQuery.chukuId" clearable class="filter-item" style="width: 200px;" placeholder="请输入对应出库台账id"/>
        <el-input v-model.number="listQuery.enterpriseId" clearable class="filter-item" style="width: 200px;" placeholder="请输入企业id"/>
      <el-input v-model="listQuery.createAt" clearable class="filter-item" style="width: 200px;" placeholder="请输入创建人"/>
      <el-date-picker v-model="listQuery.createBy" type="datetime" placeholder="请输入创建日期"></el-date-picker>
      <el-input v-model="listQuery.updateAt" clearable class="filter-item" style="width: 200px;" placeholder="请输入更新人"/>
      <el-date-picker v-model="listQuery.updateBy" type="datetime" placeholder="请输入更新日期"></el-date-picker>
      <el-button v-permission="[':wLabel:read']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="[':wLabel:create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column align="right" label="标签代码" prop="qrCode" />
      <el-table-column align="right" label="危废编号" prop="wasteId" />
      <el-table-column align="right" label="容器类别" prop="caskId" />
      <el-table-column align="right" label="容器序列号" prop="fmacno" />
      <el-table-column align="right" label="危险类别" prop="dangerType" />
      <el-table-column align="right" label="主要成分" prop="component" />
      <el-table-column align="right" label="入库重量" prop="chanshengWeight" />
      <el-table-column align="right" label="出库重量" prop="chukuWeight" />
      <el-table-column align="right" label="是否产生" prop="isChansheng" />
      <el-table-column align="right" label="是否入库" prop="isRuku" />
      <el-table-column align="right" label="是否出库" prop="isChuku" />
      <el-table-column align="right" label="是否报废" prop="isBaofei" />
      <el-table-column align="right" label="入库人" prop="rukuPerson" />
      <el-table-column :formatter="datetimeFormat"align="right" label="入库时间" prop="rukuDate" />
      <el-table-column align="right" label="对应入库台账id" prop="rukuId" />
      <el-table-column align="right" label="出库人" prop="chukuPerson" />
      <el-table-column :formatter="datetimeFormat"align="right" label="出库日期" prop="chukuDate" />
      <el-table-column align="right" label="对应出库台账id" prop="chukuId" />
      <el-table-column align="right" label="企业id" prop="enterpriseId" />
      <el-table-column align="right" label="创建人" prop="createAt" />
      <el-table-column :formatter="datetimeFormat"align="right" label="创建日期" prop="createBy" />
      <el-table-column align="right" label="更新人" prop="updateAt" />
      <el-table-column :formatter="datetimeFormat"align="right" label="更新日期" prop="updateBy" />
      <el-table-column align="center" label="操作员" prop="foperator" />
      <el-table-column align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button v-permission="[':wLabel:update']" type="primary" size="mini" class="button-margin" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="[':wLabel:delete']" type="danger" size="mini" class="button-margin" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="标签代码" prop="qrCode">
          <el-input v-model="dataForm.qrCode"/>
        </el-form-item>
        <el-form-item label="危废编号" prop="wasteId">
          <el-input v-model.number="dataForm.wasteId"/>
        </el-form-item>
        <el-form-item label="容器类别" prop="caskId">
          <el-input v-model.number="dataForm.caskId"/>
        </el-form-item>
        <el-form-item label="容器序列号" prop="fmacno">
          <el-input v-model="dataForm.fmacno"/>
        </el-form-item>
        <el-form-item label="危险类别" prop="dangerType">
          <el-input v-model="dataForm.dangerType"/>
        </el-form-item>
        <el-form-item label="主要成分" prop="component">
          <el-input v-model="dataForm.component"/>
        </el-form-item>
        <el-form-item label="入库重量" prop="chanshengWeight">
          <el-input v-model="dataForm.chanshengWeight"/>
        </el-form-item>
        <el-form-item label="出库重量" prop="chukuWeight">
          <el-input v-model="dataForm.chukuWeight"/>
        </el-form-item>
        <el-form-item label="是否产生" prop="isChansheng">
          <el-input v-model.number="dataForm.isChansheng"/>
        </el-form-item>
        <el-form-item label="是否入库" prop="isRuku">
          <el-input v-model.number="dataForm.isRuku"/>
        </el-form-item>
        <el-form-item label="是否出库" prop="isChuku">
          <el-input v-model.number="dataForm.isChuku"/>
        </el-form-item>
        <el-form-item label="是否报废" prop="isBaofei">
          <el-input v-model.number="dataForm.isBaofei"/>
        </el-form-item>
        <el-form-item label="入库人" prop="rukuPerson">
          <el-input v-model="dataForm.rukuPerson"/>
        </el-form-item>
        <el-form-item label="入库时间" prop="rukuDate">
          <el-date-picker v-model="dataForm.rukuDate" type="datetime"/>
        </el-form-item>
        <el-form-item label="对应入库台账id" prop="rukuId">
          <el-input v-model.number="dataForm.rukuId"/>
        </el-form-item>
        <el-form-item label="出库人" prop="chukuPerson">
          <el-input v-model="dataForm.chukuPerson"/>
        </el-form-item>
        <el-form-item label="出库日期" prop="chukuDate">
          <el-date-picker v-model="dataForm.chukuDate" type="datetime"/>
        </el-form-item>
        <el-form-item label="对应出库台账id" prop="chukuId">
          <el-input v-model.number="dataForm.chukuId"/>
        </el-form-item>
        <el-form-item label="企业id" prop="enterpriseId">
          <el-input v-model.number="dataForm.enterpriseId"/>
        </el-form-item>
        <el-form-item label="创建人" prop="createAt">
          <el-input v-model="dataForm.createAt"/>
        </el-form-item>
        <el-form-item label="创建日期" prop="createBy">
          <el-date-picker v-model="dataForm.createBy" type="datetime"/>
        </el-form-item>
        <el-form-item label="更新人" prop="updateAt">
          <el-input v-model="dataForm.updateAt"/>
        </el-form-item>
        <el-form-item label="更新日期" prop="updateBy">
          <el-date-picker v-model="dataForm.updateBy" type="datetime"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listwLabel, createwLabel, updatewLabel, deletewLabel } from '@/api/dangerousWaste/wLabel'
import Pagination from '@/components/Pagination'
import moment from 'moment'
export default {
  name: 'WLabel',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        qrCode: undefined,
        wasteId: undefined,
        caskId: undefined,
        fmacno: undefined,
        dangerType: undefined,
        component: undefined,
        chanshengWeight: undefined,
        chukuWeight: undefined,
        isChansheng: undefined,
        isRuku: undefined,
        isChuku: undefined,
        isBaofei: undefined,
        rukuPerson: undefined,
        rukuDate: undefined,
        rukuId: undefined,
        chukuPerson: undefined,
        chukuDate: undefined,
        chukuId: undefined,
        enterpriseId: undefined,
        createAt: undefined,
        createBy: undefined,
        updateAt: undefined,
        updateBy: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        qrCode: undefined,
        wasteId: undefined,
        caskId: undefined,
        fmacno: undefined,
        dangerType: undefined,
        component: undefined,
        chanshengWeight: undefined,
        chukuWeight: undefined,
        isChansheng: undefined,
        isRuku: undefined,
        isChuku: undefined,
        isBaofei: undefined,
        rukuPerson: undefined,
        rukuDate: undefined,
        rukuId: undefined,
        chukuPerson: undefined,
        chukuDate: undefined,
        chukuId: undefined,
        enterpriseId: undefined,
        createAt: undefined,
        createBy: undefined,
        updateAt: undefined,
        updateBy: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        qrCode: [
          { required: true, message: '标签代码不能为空' },


        ],
        wasteId: [
          { required: true, message: '危废编号不能为空' },
          { type: 'number', message: '危废编号必须为数字值' },

        ],
        caskId: [
          { required: true, message: '容器类别不能为空' },
          { type: 'number', message: '容器类别必须为数字值' },

        ],
        fmacno: [
          { required: true, message: '容器序列号不能为空' },


        ],
        dangerType: [



        ],
        component: [



        ],
        chanshengWeight: [



        ],
        chukuWeight: [



        ],
        isChansheng: [

          { type: 'number', message: '是否产生必须为数字值' },

        ],
        isRuku: [

          { type: 'number', message: '是否入库必须为数字值' },

        ],
        isChuku: [

          { type: 'number', message: '是否出库必须为数字值' },

        ],
        isBaofei: [

          { type: 'number', message: '是否报废必须为数字值' },

        ],
        rukuPerson: [



        ],
        rukuDate: [
          { required: true, message: '入库时间不能为空' },


        ],
        rukuId: [
          { required: true, message: '对应入库台账id不能为空' },
          { type: 'number', message: '对应入库台账id必须为数字值' },

        ],
        chukuPerson: [



        ],
        chukuDate: [



        ],
        chukuId: [

          { type: 'number', message: '对应出库台账id必须为数字值' },

        ],
        enterpriseId: [

          { type: 'number', message: '企业id必须为数字值' },

        ],
        createAt: [



        ],
        createBy: [



        ],
        updateAt: [



        ],
        updateBy: [



        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listwLabel(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        qrCode: undefined,
        wasteId: undefined,
        caskId: undefined,
        fmacno: undefined,
        dangerType: undefined,
        component: undefined,
        chanshengWeight: undefined,
        chukuWeight: undefined,
        isChansheng: undefined,
        isRuku: undefined,
        isChuku: undefined,
        isBaofei: undefined,
        rukuPerson: undefined,
        rukuDate: undefined,
        rukuId: undefined,
        chukuPerson: undefined,
        chukuDate: undefined,
        chukuId: undefined,
        enterpriseId: undefined,
        createAt: undefined,
        createBy: undefined,
        updateAt: undefined,
        updateBy: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createwLabel(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加b_label 标签成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatewLabel(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新b_label 标签成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除b_label 标签', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletewLabel(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除b_label 标签成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
